package QianFeng03;

import java.util.Arrays;

/**
 * 对象数组与管理
 * 使用对象数组实现多个Chicken的管理
 * 动态数组：
 * 1.数组是一种线性数据结构
 * 2.数组不适合作删除插入等操作，适合添加，查找和遍历
 */
public class Test10 {
    public static void main(String[] args) {
        ChickenManager cm = new ChickenManager(5);
        //添加小鸡
        cm.add(new Chicken(1, "涛宝", 10));
        cm.add(new Chicken(2, "熙宝", 15));
        cm.add(new Chicken(3, "欣宝", 5));
        cm.add(new Chicken(4, "杰宝", 1));
        cm.add(new Chicken(5, "裴宝", 7));

        cm.add(new Chicken(1, "黄宝", 9));//数组扩充可添加进入
        //查看数组是否扩充
        System.out.println("数组的长度是：" + cm.length());
        //输出所有小鸡
        System.out.println("---------------printAll---------------");
        cm.printAll();
        //查找小鸡
        System.out.println("---------------find---------------");//分隔线
        Chicken chicken5 = cm.findById(5);
        chicken5.println();
        //更改小鸡
        System.out.println("---------------update---------------");//分隔线
        Chicken chicken1 = new Chicken(1, "涛涛子", 11);
        cm.update(chicken1);//或直接 cm.update(new Chicken(1,"涛涛子",11))
        cm.findById(1).println();//第一只小鸡数据更改成功
        //删除小鸡
        System.out.println("---------------delete---------------");

    }
}

//小鸡管理类
class ChickenManager {
    private Chicken[] cs = null;
    private int count = 0;//记录当前数组元素的个数(下标)

    //定义数组的长度
    public ChickenManager(int size) {
        if (size > 0) {
            cs = new Chicken[size];
        } else {
            cs = new Chicken[5];
        }
    }

    //添加:实现动态数组
    public void add(Chicken c) {
        if (count >= cs.length) {//数组已经满了，需要扩充
            //算法1：扩充原来数组大小的一半  cs.length*3/2+1
            //算法2：扩充原来数组的一倍  cs.length*2
            int newLen = cs.length * 3 / 2 + 1;
            cs = Arrays.copyOf(cs, newLen);
        }
        cs[count] = c;
        count++;
    }

    //删除
    public void delete(int id) {
        for (int i = 0; i < count; i++) {
            if (cs[i].getId() == id) {
                //找到了要删除的对象，把该对象之后的对象向前移动一位
                for (int j = 0; j < count - 1; j++) {
                    cs[j] = cs[j + 1];
                }
                //把最后一个对象赋值为空（删除）
                cs[count - 1] = null;
                count--;//下标减一
                break;
            }
        }
    }

    //更新
    public void update(Chicken chicken) {
        Chicken temp = findById(chicken.getId());
        if (temp != null) {
            temp.setName(chicken.getName());
            temp.setAge(chicken.getAge());
        }
    }

    //通过id查询
    public Chicken findById(int id) {
        for (int i = 0; i < count; i++) {
            if (cs[i].getId() == id) {
                return cs[i];
            }
        }
        return null;
    }

    //输出所有
    public void printAll() {
        for (int i = 0; i < count; i++) {
            cs[i].println();
        }
    }

    //显示数组的长度
    public int length() {
        return cs.length;
    }
}

//小鸡类（数据对象)  value object (vo)
class Chicken {
    private int id;
    private String name;
    private int age;

    //一般情况下最好保存默认的构造方法
    public Chicken() {
    }

    public Chicken(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public void println() {
        System.out.println("id:" + id + ",name:" + name + ",age:" + age);
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}